package com.asman.worker.push

import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.util.Log
import com.alibaba.sdk.android.push.MessageReceiver
import com.alibaba.sdk.android.push.notification.CPushMessage
import com.asman.worker.MainActivity
import com.asman.worker.MainApplication
import com.asman.worker.rn.NativeEventUtils

/**
 * 阿里消息推送
 */
class MyMessageReceiver : MessageReceiver() {

    companion object {
        // 消息接收部分的LOG_TAG
        val REC_TAG = "receiver"
    }

    public override fun onNotification(
        context: Context?, title: String?, summary: String?, extraMap: Map<String, String>?
    ) {
        //处理推送通知
        Log.e(
            "MyMessageReceiver",
            "Receive notification, title: $title, summary: $summary, extraMap: $extraMap"
        )
    }

    public override fun onMessage(context: Context?, cPushMessage: CPushMessage?) {
        Log.e(
            "MyMessageReceiver",
            "onMessage, messageId: " + cPushMessage!!.messageId + ", title: " + cPushMessage.title + ", content:" + cPushMessage.content
        )
    }

    public override fun onNotificationOpened(
        context: Context?, title: String?, summary: String?, extraMap: String?
    ) {

//        val msgData = Gson().fromJson(extraMap, MessageData::class.java)
//        //点击通知
//        context?.let { IntentUtils.intentARouter(it, msgData.actionUrl) }
        val currentReactContext =
            (context?.applicationContext as MainApplication).reactNativeHost.reactInstanceManager.currentReactContext

        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)

        NativeEventUtils.nativeNotifClick(currentReactContext, extraMap)

        Log.e(
            "MyMessageReceiver",
            "onNotificationOpened, title: $title, summary: $summary, extraMap:$extraMap"
        )
    }

    override fun onNotificationClickedWithNoAction(
        context: Context?, title: String?, summary: String?, extraMap: String?
    ) {
        Log.e(
            "MyMessageReceiver",
            "onNotificationClickedWithNoAction, title: $title, summary: $summary, extraMap:$extraMap"
        )
    }

    override fun onNotificationReceivedInApp(
        context: Context?,
        title: String?,
        summary: String?,
        extraMap: Map<String, String>?,
        openType: Int,
        openActivity: String?,
        openUrl: String?
    ) {
        Log.e(
            "MyMessageReceiver",
            "onNotificationReceivedInApp, title: $title, summary: $summary, extraMap:$extraMap, openType:$openType, openActivity:$openActivity, openUrl:$openUrl"
        )
    }

    override fun onNotificationRemoved(context: Context?, messageId: String?) {
        Log.e("MyMessageReceiver", "onNotificationRemoved")
    }

}
